#include "stdafx.h"
#include "Framework\lego_3Dview.h"
#include "Framework\lego_3Ddoc.h"
#include "DLG_CubeAnalyzer.h"
#include "TriangleMesh\Partition\OctreePartitioner.h"
#include "TriangleMesh\structure\Enriched_polyhedron.h"

#pragma warning(disable : 4800)

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CDLG_CubeAnalyzer::CDLG_CubeAnalyzer(CWnd* pParent /*=NULL*/, CLego_3Dview *pView /*= NULL*/)
	: CDialog(CDLG_CubeAnalyzer::IDD, pParent)
{
	m_pView = pView;
	CDialog::Create(CDLG_CubeAnalyzer::IDD);
}

CDLG_CubeAnalyzer::CDLG_CubeAnalyzer(CLego_3Dview *pView)
{
	m_pView = pView;
	CDialog::Create(CDLG_CubeAnalyzer::IDD);
}


void CDLG_CubeAnalyzer::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_OCTREE_MAX_DEPTH, EDTMaxDepthOctree);
	DDX_Control(pDX, IDC_CHECK_SHOW_OCTREE,		CHKDrawOctree);
	//{{AFX_DATA_MAP(COpenGLDialogDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDLG_CubeAnalyzer, CDialog)
	//{{AFX_MSG_MAP(COpenGLDialogDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_OK, OnOk)
	ON_BN_CLICKED(IDC_BUTTON_BUILD_OCTREE,	OnBuildOcTreePartition)
	ON_BN_CLICKED(IDC_CHECK_SHOW_OCTREE,	OnCHKDrawOcTreePartition)
	ON_BN_CLICKED(IDC_BUTTON_WATCH_CUBE,	OnBtnWatchPickedCube)		
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CDLG_CubeAnalyzer::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			
	SetIcon(m_hIcon, FALSE);	
			
	CRect rect;

	GetDlgItem(IDC_OPENGL_WINDOW)->GetWindowRect(rect);

	ScreenToClient(rect);
	
	openGLControl.Create(rect,this);

	return TRUE;
}

void CDLG_CubeAnalyzer::OnPaint() 
{
	CDialog::OnPaint();	
}

HCURSOR CDLG_CubeAnalyzer::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDLG_CubeAnalyzer::OnOk() 
{
	CDialog::OnOK();	
}

void CDLG_CubeAnalyzer::Re_display()
{
	m_pDoc = (CLego_3Ddoc*)m_pView->GetDocument(); 
	CWnd::ShowWindow(SW_SHOW);
}

void CDLG_CubeAnalyzer::OnBuildOcTreePartition()
{	
	CString s;
	EDTMaxDepthOctree.GetWindowText(s);
	m_pDoc->m_SpaceParMaxDepth = atoi(s);
	m_pDoc->OnOctreeNew();
}

void CDLG_CubeAnalyzer::OnCHKDrawOcTreePartition()
{	
	m_pView->m_bDrawSpacePar = CHKDrawOctree.GetCheck();
	m_pView->InvalidateRect(NULL, FALSE);
}

void CDLG_CubeAnalyzer::OnBtnWatchPickedCube()
{	
	if( m_pDoc->m_pMesh->nb_picked_vertices()!=1 )
		return;
	int pickVid = m_pDoc->m_pMesh->picked_vertices.front();
	int pickFid = m_pDoc->m_pMesh->picked_facets.front();
	OctreePartitioner *pOctree =  dynamic_cast<OctreePartitioner*> (m_pDoc->m_pSpacePar);
	
	m_pView->PickObjType = Cube;

	int nNELeafNodes = pOctree->m_NE_Leaves.size();
	m_pView->InvalidateRect(NULL, FALSE);
}